import Cookies from 'js-cookie'

/**
 * 
 * @param {服务端上下文} app 
 * @param {值名} name 
 * @param {值内容} value 
 */
export function setItem(key, value) {
  if (process.client && value) {
    var isToken = key === "token"
    if(!isToken){
      localStorage.setItem(key, JSON.stringify(value))
    }else{
      Cookies.set(key, value, { expires: 30 })
    }
  }
}

/**
 * 
 * @param {值名} key 
 * @returns 
 */
export function getItem(key) {
  //从获取token
  let value = null

  if (process.client && key !== "token") {
    var cookieValue = localStorage.getItem(key)
    if(cookieValue && cookieValue != undefined && cookieValue != null){
      value = JSON.parse(cookieValue);
    }
  }

  return value
}

/**
 * 
 * @param {值名} name 
 */
export function clear() {
  if (process.client) {
    Cookies.remove("token")
    localStorage.removeItem("USER_INFO")
  }
}

export default{
  setItem,
  getItem,
  clear
}